package com.example.teacher.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.teacher.R
import com.example.teacher.data.Attendance

/**
 *@Created by 覃德玮 on 2021/9/6 10:32
 *@Email: chadewik@foxmail.com
 *@Description:
 */
class RecyclerListAdapter(mContext:Context,listData: List<Attendance>) :
    RecyclerView.Adapter<RecyclerListAdapter.RecyclerHolder>() {
    val data:List<Attendance> = listData
    private var context: Context = mContext
    private var itemClickListener: RecyclerItemClickListener? = null


    class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val courseName: TextView = itemView.findViewById(R.id.name)
        val courseId: TextView =itemView.findViewById(R.id.courseIdtv)
        val time: TextView = itemView.findViewById(R.id.time)
        val courseNum: TextView = itemView.findViewById(R.id.attendance)
        val absentNum: TextView = itemView.findViewById(R.id.exitnum)
    }

    //自定义接口
    interface RecyclerItemClickListener {
        fun onItemClickListener(position: Int)
    }

    fun setOnRecyclerItemClickListener(itemClickListener: RecyclerItemClickListener) {
        this.itemClickListener = itemClickListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerHolder {
        var view =
            LayoutInflater.from(context).inflate(R.layout.list_item, parent, false)
        return RecyclerHolder(view)
    }


    override fun getItemCount(): Int {
        return data.size
    }

    override fun onBindViewHolder(holder: RecyclerHolder, position: Int) {
        holder.courseId.text=data[position].courseId
        holder.courseName.text=data[position].courseName
        holder.time.text=data[position].time
        holder.courseNum.text=data[position].number.toString()
        holder.absentNum.text=data[position].absentNum.toString()
        holder.itemView.setOnClickListener {
            itemClickListener!!.onItemClickListener(position)
        }
    }


}